package touro.mancala.client;


import java.awt.Color;

//import javax.swing.JColorChooser;
//import javax.swing.JOptionPane;

public class GameState {

	private static GameState singleton = new GameState();
	
	public static GameState getGameState() {
		return singleton;
	}
	
	private GameState() {
		String playerOne = "0"; //JOptionPane.showInputDialog("What is the first player's name?");
		String playerTwo = "1"; //JOptionPane.showInputDialog("What is the second player's name?");
		players = new Player[]{new Player(playerOne), new AIPlayer(playerTwo)};
		color = null; //JColorChooser.showDialog(null, "Choose color of stones. To display random-colored stones, click cancel.", null);
		board = new Board(players, color);
		activePlayer = players[0];
	}
	
	private Player players[];
	private Board board;
	private Player activePlayer;
	private Color color;
	
	public Player nextPlayer () {
		activePlayer = (activePlayer.equals(players[0])) ? players[1] : players[0];
		return activePlayer;
	}

	public Player[] getPlayers() {
		return players;
	}

	public Board getBoard() {
		return board;
	}

	public Player getActivePlayer() {
		return activePlayer;
	}
	
	//returns -1 if game is not over
	//returns 1 if first player is empty
	//returns 0 if second player is empty
	public int isGameOver () {
		if (board.isEmpty(players[0])) {
			return 1;
		} else if (board.isEmpty(players[1])) {
			return 0;
		} else {
			return -1;
		}
	}
	
	public Player mostStones () {
		int firstPlayer = (board.getPitsAndStores())[6].getSize();
		int secondPlayer = (board.getPitsAndStores())[13].getSize();
		if (firstPlayer > secondPlayer) {
			return players[0];
		} else if (secondPlayer > firstPlayer) {
			return players[1];
		} else {
			return null;
		}
	}
	
	//this returns the color that was chosen by the user
	//if the user did not choose any color because the user wanted random coloring, the method returns Color.WHITE
	public Color getColor() {
		if (color==null) {
			return Color.WHITE;
		}
		return color;
	}
	
	
}
